package com.andy.infrastructure.util;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

public class StringCaseUtil {
    //识别驼峰大写字母的正则
    private static final Pattern HUMP_PATTERN = Pattern.compile("[A-Z]");
    private static final Pattern APL_PATTERN = Pattern.compile("[A-Za-z]");
    private static final Pattern CHINESE_PATTERN = Pattern.compile("[\\u4e00-\\u9fa5]");


    /**
     * 字符串转为3种不同的形式
     * @param input
     * @return
     */
    public static Map<String,String> transferStr(String input){
        Map<String,String> result = new HashMap <>();

        //空入参返回空map
        if(StringUtils.isBlank(input)){
            result.put("I_I","");
            result.put("Iii","");
            result.put("iii","");
            return result;
        }

        //包含中文 或 空格 或压根没有一个英文字母的字符串不做处理
        Matcher matcher = CHINESE_PATTERN.matcher(input);
        Matcher aplMatcher = APL_PATTERN.matcher(input);
        if(matcher.find() || input.contains(" ") || !aplMatcher.find()){
            result.put("I_I",input);
            result.put("Iii",input);
            result.put("iii",input);
            return result;
        }

        //1-转成数组
        String[] strArr = strToUnderline(input);

        //2-构造3种字符串
        StringBuffer IiiSb = new StringBuffer();
        StringBuffer I_ISb = new StringBuffer();

        for(String str:strArr){
            String strLow = str.toLowerCase();
            String strUp = str.toUpperCase();

            IiiSb.append(strLow.substring(0,1).toUpperCase()+strLow.substring(1));
            I_ISb.append("_"+strUp);
        }

        //3-拿到正确的字符串
        String I_I = I_ISb.toString().substring(1);
        String Iii = IiiSb.toString();
        String iii = Iii.substring(0,1).toLowerCase()+Iii.substring(1);

        //4-返回
        result.put("I_I",I_I);
        result.put("Iii",Iii);
        result.put("iii",iii);



        return result;

    }

    /**
     * 字符串转数组
     * @param str
     * @return
     */
    private static String[] strToUnderline(String str){
        //1-转为下划线
        String underline;

        //2-转为下划线
        if(isUnderline(str)){
            underline = str;
        }else if(isOneWord(str)){
            underline = str;
        }else{
            underline = camelToUnderline(str);
        }

        //3-转为数组
        String[] strArr = underline.split("_");
        return strArr;
    }

    /**
     * 驼峰转下划线
     * @param camelStr
     * @return
     */
    private static String camelToUnderline(String camelStr){
        //PlayGame => play_game
        if(StringUtils.isBlank(camelStr)) return camelStr;

        //1-如果首字母是大写则转化为小写
        camelStr = camelStr.substring(0,1).toLowerCase()+camelStr.substring(1);

        //2-将大写字母转化为_x : xxXxx => xx_xxx
        Matcher matcher = HUMP_PATTERN.matcher(camelStr);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        String underline = sb.toString();

        return underline;
    }

    /**
     * 是否下划线
     * @param str
     * @return
     */
    private static boolean isUnderline(String str){
        if(StringUtils.isBlank(str)) return true;

        if(str.indexOf("_")>0) return true;

        return false;
    }

    private static boolean isOneWord(String str){
        if(StringUtils.isBlank(str)) return true;

        boolean hasBig = false;
        boolean hasSmall = false;

        char[] chars = str.toCharArray();
        for(char c:chars){
            Matcher matcher = HUMP_PATTERN.matcher(String.valueOf(c));
            if(matcher.matches()){
                hasBig = true;
            }else{
                hasSmall = true;
            }
        }

        return !(hasBig&&hasSmall);
    }


}
